package com.stuinfo;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class AllWay {

	public static void wayUpdate(Connection con) {
		String sql_update = "update stuinfo set name=?,sex=? where id=?";
		try {
			PreparedStatement pre_update = con.prepareStatement(sql_update);
			Scanner sca = new Scanner(System.in);
			int i;
			String n, s;
			System.out.println("请输入要修改得学生ID");
			i = sca.nextInt();
			System.out.println("请输入要修改的学生的姓名");
			n = sca.next();
			System.out.println("请输入你要修改的学生性别");
			s = sca.next();

			pre_update.setString(1, n);
			pre_update.setString(2, s);
			pre_update.setInt(3, i);

			pre_update.executeQuery();
			System.out.println("修改学生信息成功");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void waySelect(Connection con) {
		String sql_select = "select * from stuinfo where id=?";

		try {
			PreparedStatement pre_select = con.prepareStatement(sql_select);
			System.out.println("请输入想查询学生ID号");
			Scanner sc = new Scanner(System.in);
			int a = sc.nextInt();
			pre_select.setInt(1, a);
			ResultSet res = pre_select.executeQuery();
			res.next();
			System.out.println(res.getInt("id") + "\t" + res.getString("name") + "\t" + res.getString("sex"));

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void allSelect(Connection conn) {
		String sql_select = "select * from stuinfo order by id";

		PreparedStatement pre_select;
		try {
			pre_select = conn.prepareStatement(sql_select);
			ResultSet resu = pre_select.executeQuery();
			while (resu.next()) {
				System.out.println(resu.getInt("id") + "\t" + resu.getString("name") + "\t" + resu.getString("sex"));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void wayDelete(Connection con) {
		String sql_delete = "delete from stuinfo where id=?";
		try {
			PreparedStatement pre_delete = con.prepareStatement(sql_delete);
			Scanner sca = new Scanner(System.in);
			System.out.println("请输入你要删除的学生ID");
			int a = sca.nextInt();
			pre_delete.setInt(1, a);
			pre_delete.executeQuery();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
